﻿using Example;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using wms_wpf.Apiservice;
using wms_wpf.Views;

namespace wms_wpf.ViewModels
{
    public class LoginViewModel : BindableBase, IDialogAware
    {
        public LoginViewModel(LoginView loginView )
        {
            LoginCommand = new DelegateCommand(Login);
            LoginSimpleCommand=new DelegateCommand(LoginSimple);
            this.LoginView = loginView;
            Service= new UserService();
        }

        public string Title { get; set; } = "wms";
        private LoginView LoginView { get; set; }
        public event Action<IDialogResult> RequestClose;

        public DelegateCommand LoginCommand { get; set; }
        public DelegateCommand LoginSimpleCommand { get; set; }
        public MyUserService Service { get; set; }
        public async void Login()
        {
         
        
            bool result= await Service.LoginUser(new InPut.InPut_UserService2() { password =LoginView.password.Password, username = UserName });
         
            if (IsCheck_RadioButton)
            {
                if (result) RequestClose?.Invoke(new DialogResult(ButtonResult.Yes));
            }
            else
            {
                if (result) RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }
        }
        
        public async void LoginSimple()
        {


            bool result = await Service.LoginUser(new InPut.InPut_UserService2() { password = LoginView.password.Password, username = UserName });
            if (result)
            {
                var MainView = new ExpressView();
                MainView.Show();
            }

        }
        private string username;
        public string  UserName
        {
            get { return username; }
            set { username = value;RaisePropertyChanged(); }
        }

        private string  password;

        public string  PassWord
        {
            get { return password; }
            set { password = value;RaisePropertyChanged(); }
        }

        private bool ischeck;
        public bool IsCheck_RadioButton
        {
            get { return ischeck; }
            set { ischeck = value; RaisePropertyChanged(); }
        }


        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            
        }
    }
}
